<?php

include_once 'DataProvider.php';
include_once 'Entity.php';

class DonHang extends Entity {

    const TRANGTHAI_CHOXULY = 1;
    const TRANGTHAI_DAXACNHAN = 2;
    const TRANGTHAI_DAGIAOHANG = 3;

    public static $TrangThaiArray = array(
        '1' => 'Chờ xử lý',
        '2' => 'Đã xác nhận',
        '3' => 'Đã giao hàng'
    );
    public $dataProvider;

    public function __construct() {
        $this->dataProvider = new DataProvider();
    }

    public function layGiaTheoID($pId) {
        $sql = "select sanpham.gia from sanpham where sanpham.id = $pId";
        $row = $this->dataProvider->GetOneRow($sql);
        return $row['gia'];
    }

    /**
     *
     * @param array $hoadonData 
     * @return boolean tra ve true neu cap nhap thanh khong nguoc lai false
     */
    public function capNhatHoaDon($hoadonData) {
        $sql = "INSERT INTO `donhang` (user,trangthai,ngaydathang) VALUES ('{$hoadonData['session_id_user']}', '" . self::TRANGTHAI_CHOXULY . "', '" . date("Y-m-d", time()) . "')";
        $flag = $this->dataProvider->ExecuteQuery($sql);
        $donHangID = DataProvider::$LAST_INSERT_ID;

        if ($flag) {
            $sql = "INSERT INTO `chitietdonhang` (`donhang_id`, `sanpham_id`, `soluong`, `dongiahientai`) VALUES ";
            $tmpArr = array();
            foreach ($hoadonData['giohang'] as $pId => $soLuong) {
                $tmpArr[] = "($donHangID, $pId, $soLuong, " . $this->layGiaTheoID($pId) . ")";
            }
            $sql .= implode(",", $tmpArr);
            if ($this->dataProvider->ExecuteQuery($sql)) {
                unset($_SESSION['giohang']);
                return true;
            }
        }
        return false;
    }

    public function layDonHang() {
        $sql = "select * from `donhang`";
        $tmp = $this->dataProvider->ExecuteQuery($sql);
        $result = array();
        while ($row = mysql_fetch_assoc($tmp)) {
            $result[] = $row;
        }
        return $result;
    }

    public function xoaDonHang($id) {
        $sql = "DELETE FROM `donhang` WHERE id = '" . $id . "'";
        return DataProvider::ExecuteQuery($sql);
    }

    public function layTenSanPham($id) {
        $sql = "SELECT ten FROM sanpham where sanpham.id = $id ";
        $result = $this->dataProvider->GetOneRow($sql);
        return $result;
    }

    public function xoaChiTietDonHang($id) {
        $sql = "DELETE FROM `chitietdonhang` WHERE sanpham_id = '" . $id . "'";
        return DataProvider::ExecuteQuery($sql);
    }

    public function capnhapDonHang($id, $data) {
        $sql = "UPDATE `donhang` 
                SET `donhang`.trangthai = '{$data['trangthai']}'
                where `donhang`.id = '$id'";
        $result = $this->dataProvider->ExecuteQuery($sql);
        return $result;
    }

    public function layDonHangCN($id) {
        $sql = "SELECT * from `donhang` WHERE `donhang`.id = '$id'";
        return DataProvider::GetOneRow($sql);
    }

    public function layChiTietThanhVien($user) {
        $sql = "SELECT * from `users` WHERE `users`.UserName = '$user'";
        return DataProvider::GetOneRow($sql);
    }

    public function layChiTietDonHang($donhang_id) {
        $sql = "SELECT * FROM chitietdonhang where chitietdonhang.donhang_id = $donhang_id ";
        $result = $this->dataProvider->ExecuteQuery($sql);
        return $result;
    }

}

?>
